﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
namespace PM.PMBusiness
{
    /// <summary>
    /// applicationform 的摘要说明
    /// </summary>
    public class applicationform
    {
        public applicationform()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        /// <summary>
        /// 增加一个项目信息（新建联系人）
        /// </summary>
        /// <param name="newproject">项目实体类</param>
        /// <param name="account">账户实体类</param>
        /// <returns>返回存储过程结果</returns>
        public int Add(PM.Model.Project newproject, PM.Model.Sys_Account account)
        {
            try
            {
                SqlParameter[] parameters = {
					new SqlParameter("@sub1", SqlDbType.NVarChar,50),
					new SqlParameter("@sub2", SqlDbType.NVarChar,50),
					new SqlParameter("@zcode", SqlDbType.NVarChar,20),
					new SqlParameter("@pcollege", SqlDbType.NVarChar,50),
					new SqlParameter("@bz", SqlDbType.NVarChar,400),
					new SqlParameter("@status", SqlDbType.SmallInt,2),
					new SqlParameter("@pmanager", SqlDbType.NVarChar,100),
					new SqlParameter("@attached", SqlDbType.NVarChar,200),
					new SqlParameter("@builddate", SqlDbType.DateTime),
					new SqlParameter("@type", SqlDbType.NVarChar,100),
					new SqlParameter("@ptypeid", SqlDbType.NVarChar,50),
					new SqlParameter("@pname", SqlDbType.NVarChar,200),
					new SqlParameter("@plevel", SqlDbType.NVarChar,20),
					new SqlParameter("@pway", SqlDbType.NVarChar,50),
					new SqlParameter("@ptype", SqlDbType.NVarChar,20),
					new SqlParameter("@pcommit", SqlDbType.NVarChar,200),
					new SqlParameter("@pnature", SqlDbType.NVarChar,20),
                    new SqlParameter("@pcode",SqlDbType.NVarChar,50),
                        new SqlParameter("@depart", SqlDbType.VarChar,100),
						new SqlParameter("@cellphone", SqlDbType.VarChar,50),
						new SqlParameter("@email", SqlDbType.VarChar,100),
						new SqlParameter("@regdatetime", SqlDbType.DateTime),
						new SqlParameter("@status1", SqlDbType.TinyInt,1),
						new SqlParameter("@tid", SqlDbType.VarChar,100),
						new SqlParameter("@name", SqlDbType.VarChar,50),
						new SqlParameter("@title", SqlDbType.VarChar,50)};
                parameters[0].Value = newproject.sub1;
                parameters[1].Value = newproject.sub2;
                parameters[2].Value = newproject.zcode;
                parameters[3].Value = newproject.pcollege;
                parameters[4].Value = newproject.bz;
                parameters[5].Value = newproject.status;
                parameters[6].Value = newproject.pmanager;
                parameters[7].Value = newproject.attached;
                parameters[8].Value = newproject.builddate;
                parameters[9].Value = newproject.type;
                parameters[10].Value = newproject.ptypeid;
                parameters[11].Value = newproject.pname;
                parameters[12].Value = newproject.plevel;
                parameters[13].Value = newproject.pway;
                parameters[14].Value = newproject.ptype;
                parameters[15].Value = newproject.pcommit;
                parameters[16].Value = newproject.pnature;
                parameters[17].Value = newproject.pcode;
                parameters[18].Value = account.depart;
                parameters[19].Value = account.cellphone;
                parameters[20].Value = account.email;
                parameters[21].Value = account.regdatetime;
                parameters[22].Value = account.status;
                parameters[23].Value = account.tid;
                parameters[24].Value = account.name;
                parameters[25].Value = account.title;

                object result = PM.Code.DAL.DbHelper.RunProcedure1("Pdmis_ProjectAdd", parameters);
                return int.Parse(result.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return 0;
        }
        /// <summary>
        /// 增加一个项目信息（已有联系人）
        /// </summary>
        /// <param name="newproject">项目实体类</param>
        /// <returns>返回存储过程结果</returns>
        public int Add(PM.Model.Project newproject)
        {
            try
            {
                SqlParameter[] parameters = {
					new SqlParameter("@sub1", SqlDbType.NVarChar,50),
					new SqlParameter("@sub2", SqlDbType.NVarChar,50),
					new SqlParameter("@zcode", SqlDbType.NVarChar,20),
					new SqlParameter("@pcollege", SqlDbType.NVarChar,50),
					new SqlParameter("@bz", SqlDbType.NVarChar,400),
					new SqlParameter("@status", SqlDbType.SmallInt,2),
					new SqlParameter("@pmanager", SqlDbType.NVarChar,100),
					new SqlParameter("@attached", SqlDbType.NVarChar,200),
					new SqlParameter("@builddate", SqlDbType.DateTime),
					new SqlParameter("@type", SqlDbType.NVarChar,100),
					new SqlParameter("@ptypeid", SqlDbType.NVarChar,50),
					new SqlParameter("@pname", SqlDbType.NVarChar,200),
					new SqlParameter("@plevel", SqlDbType.NVarChar,20),
					new SqlParameter("@pway", SqlDbType.NVarChar,50),
					new SqlParameter("@ptype", SqlDbType.NVarChar,20),
					new SqlParameter("@pcommit", SqlDbType.NVarChar,200),
					new SqlParameter("@pnature", SqlDbType.NVarChar,20),
                    new SqlParameter("@pcode",SqlDbType.NVarChar,50),
                    new SqlParameter("@yeardue",SqlDbType.SmallInt)};
                parameters[0].Value = newproject.sub1;
                parameters[1].Value = newproject.sub2;
                parameters[2].Value = newproject.zcode;
                parameters[3].Value = newproject.pcollege;
                parameters[4].Value = newproject.bz;
                parameters[5].Value = newproject.status;
                parameters[6].Value = newproject.pmanager;
                parameters[7].Value = newproject.attached;
                parameters[8].Value = newproject.builddate;
                parameters[9].Value = newproject.type;
                parameters[10].Value = newproject.ptypeid;
                parameters[11].Value = newproject.pname;
                parameters[12].Value = newproject.plevel;
                parameters[13].Value = newproject.pway;
                parameters[14].Value = newproject.ptype;
                parameters[15].Value = newproject.pcommit;
                parameters[16].Value = newproject.pnature;
                parameters[17].Value = newproject.pcode;
                parameters[18].Value = newproject.yeardue;
                object result = PM.Code.DAL.DbHelper.RunProcedure1("Pdmis_ProjectAddWithin", parameters);
                return Int32.Parse(result.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return 0;
        }
        /// <summary>
        /// 获取申报计划的类型，级别，项目类型，以及检测是否存在
        /// </summary>
        /// <param name="typeid">类型编号</param>
        /// <param name="date">现在时间</param>
        /// <param name="ptype">要绑定的项目类型</param>
        /// <param name="plevel">要绑定的项目级别</param>
        /// <param name="type">类型名称</param>
        /// <returns>返回plancode</returns>
        public string getPlancode(string typeid,string date,DropDownList ptype,DropDownList plevel,Label type)
        {
            string plancode = "none";
            StringBuilder sqlstr = new StringBuilder("select plancode,btnname,plevel,ptype from View_ProjectPlan where ");
            sqlstr.Append("opendate<='").Append(date).Append("' and closedate>'").Append(date).Append("' ");
            sqlstr.Append("and type='").Append(typeid.Trim()).Append("' and pstatus='已完成'");
            try
            {
                SqlDataReader myreader = PM.Code.DAL.DbHelper.ExecuteDataReaderBySql(sqlstr.ToString());
                if (myreader.HasRows)
                {
                    if (myreader.Read())
                    {
                        plancode = myreader[0].ToString();
                        type.Text = myreader[1].ToString();
                        if (myreader[2].ToString().Trim() !="无限制")
                        {
                            plevel.SelectedValue = myreader[2].ToString().Trim();
                            plevel.Enabled = false;
                        }
                        if (myreader[3].ToString().Trim() != "无限制")
                        {
                            ptype.SelectedValue = myreader[3].ToString().Trim();
                            ptype.Enabled = false;
                        }
                    }
                }
                return plancode;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 删除一项未审批的项目
        /// </summary>
        /// <param name="id">项目编号</param>
        /// <returns></returns>
        public int Delete_Project(string id)
        {
            try
            {
                StringBuilder sqlstr1 = new StringBuilder();
                sqlstr1.Append("update Bus_Project set status=30 where id=").Append(id);
                return PM.Code.DAL.DbHelper.ExecuteSql(sqlstr1.ToString());
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }
    }
}
